home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / gcl-1.000 / gcl-1 / gcl-1.0 / lsp / listlib.lsp < prev    next >
Encoding:
Lisp/Scheme  |  1994-05-07  |  6.8 KB  |  177 lines

  1. ;; Copyright (C) 1994 M. Hagiya, W. Schelter, T. Yuasa
  2.  
  3. ;; This file is part of GNU Common Lisp, herein referred to as GCL
  4. ;;
  5. ;; GCL is free software; you can redistribute it and/or modify it under
  6. ;;  the terms of the GNU LIBRARY GENERAL PUBLIC LICENSE as published by
  7. ;; the Free Software Foundation; either version 2, or (at your option)
  8. ;; any later version.
  9. ;; 
  10. ;; GCL is distributed in the hope that it will be useful, but WITHOUT
  11. ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. ;; FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public 
  13. ;; License for more details.
  14. ;; 
  15. ;; You should have received a copy of the GNU Library General Public License 
  16. ;; along with GCL; see the file COPYING.  If not, write to the Free Software
  17. ;; Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.  
  20. ;;;;    listlib.lsp
  21. ;;;;
  22. ;;;;                        list manipulating routines
  23.  
  24. ; Rewritten 11 Feb 1993 by William Schelter and Gordon Novak to use iteration
  25. ; rather than recursion, as needed for large data sets.
  26.  
  27.  
  28. (in-package 'lisp)
  29.  
  30. (export '(union nunion intersection nintersection
  31.           set-difference nset-difference set-exclusive-or nset-exclusive-or
  32.           subsetp))
  33.  
  34. (in-package 'system)
  35.  
  36. (eval-when (compile)
  37.   (proclaim '(optimize (safety 0) (space 3)))
  38.   )
  39.  
  40. ;(defun union (list1 list2 &rest rest &key test test-not key)
  41. ;  (declare (ignore test test-not key))
  42. ;  (cond ((null list1) list2)
  43. ;        ((apply #'member1 (car list1) list2 rest)
  44. ;         (apply #'union (cdr list1) list2 rest))
  45. ;        (t
  46. ;         (cons (car list1)
  47. ;               (apply #'union (cdr list1) list2 rest)))))
  48. (defun union (list1 list2 &rest rest &aux first last)
  49.   (do ((x list1 (cdr x)))
  50.       ((null x) (if last (rplacd last list2)) (return (or first list2)))
  51.     (or (consp x) (error "UNION not passed a list"))
  52.     (if (not (apply #'member1 (car x) list2 rest))
  53.     (if last (progn (rplacd last (cons (car x) nil))
  54.             (setq last (cdr last)))
  55.              (progn (setq first (cons (car x) nil))
  56.             (setq last first)))) ) )
  57.  
  58. ;(defun nunion (list1 list2 &rest rest &key test test-not key)
  59. ;  (declare (ignore test test-not key))
  60. ;  (cond ((null list1) list2)
  61. ;        ((apply #'member1 (car list1) list2 rest)
  62. ;         (apply #'nunion (cdr list1) list2 rest))
  63. ;        (t
  64. ;         (rplacd list1
  65. ;                 (apply #'nunion (cdr list1) list2 rest)))))
  66. (defun nunion (list1 list2 &rest rest &aux first last)
  67.   (do ((x list1 (cdr x)))
  68.       ((null x) (if last (rplacd last list2)) (return (or first list2)))
  69.     (or (consp x) (error "NUNION not passed a list"))
  70.     (if (not (apply #'member1 (car x) list2 rest))
  71.     (progn (if last (rplacd last x)
  72.                 (setq first x))
  73.            (setq last x))) ) )
  74.  
  75. ;(defun intersection (list1 list2 &rest rest &key test test-not key)
  76. ;  (declare (ignore test test-not key))
  77. ;  (cond ((null list1) nil)
  78. ;        ((apply #'member1 (car list1) list2 rest)
  79. ;         (cons (car list1)
  80. ;               (apply #'intersection (cdr list1) list2 rest)))
  81. ;        (t (apply #'intersection (cdr list1) list2 rest))))
  82.  
  83. ;; all functions in this file should be written as follows:
  84. ;; Besides being non recursive, it allows compilation on safety 0
  85. (defun intersection (list1 list2 &rest rest &aux ans)
  86.   (do ((x list1 (cdr x)))
  87.       ((null x) (return ans))
  88.     (or (consp x) (error "INTERSECTION not passed a list"))
  89.     (if (apply #'member1 (car x) list2 rest)
  90.         (setq ans (cons (car x) ans))))
  91.   )
  92.  
  93. ;(defun nintersection (list1 list2 &rest rest &key test test-not key)
  94. ;  (declare (ignore test test-not key))
  95. ;  (cond ((null list1) nil)
  96. ;        ((apply #'member1 (car list1) list2 rest)
  97. ;         (rplacd list1
  98. ;                 (apply #'nintersection (cdr list1) list2 rest)))
  99. ;        (t (apply #'nintersection (cdr list1) list2 rest))))
  100. (defun nintersection (list1 list2 &rest rest &aux first last)
  101.   (do ((x list1 (cdr x)))
  102.       ((null x) (if last (rplacd last nil)) (return first))
  103.     (or (consp x) (error "NINTERSECTION not passed a list"))
  104.     (if (apply #'member1 (car x) list2 rest)
  105.     (progn (if last (rplacd last x)
  106.                 (setq first x))
  107.            (setq last x))) ) )
  108.  
  109. ;(defun set-difference (list1 list2 &rest rest &key test test-not key)
  110. ;  (declare (ignore test test-not key))
  111. ;  (cond ((null list1) nil)
  112. ;        ((not (apply #'member1 (car list1) list2 rest))
  113. ;         (cons (car list1)
  114. ;               (apply #'set-difference (cdr list1) list2 rest)))
  115. ;        (t (apply #'set-difference (cdr list1) list2 rest))))
  116. (defun set-difference (list1 list2 &rest rest &aux ans)
  117.   (do ((x list1 (cdr x)))
  118.       ((null x) (return ans))
  119.     (or (consp x) (error "SET-DIFFERENCE not passed a list"))
  120.     (if (not (apply #'member1 (car x) list2 rest))
  121.         (setq ans (cons (car x) ans))))  )
  122.  
  123. ;(defun nset-difference (list1 list2 &rest rest &key test test-not key)
  124. ;  (declare (ignore test test-not key))
  125. ;  (cond ((null list1) nil)
  126. ;        ((not (apply #'member1 (car list1) list2 rest))
  127. ;         (rplacd list1
  128. ;                 (apply #'nset-difference (cdr list1) list2 rest)))
  129. ;        (t (apply #'nset-difference (cdr list1) list2 rest))))
  130. (defun nset-difference (list1 list2 &rest rest &aux first last)
  131.   (do ((x list1 (cdr x)))
  132.       ((null x) (if last (rplacd last nil)) (return first))
  133.     (or (consp x) (error "NSET-DIFFERENCE not passed a list"))
  134.     (if (not (apply #'member1 (car x) list2 rest))
  135.     (progn (if last (rplacd last x)
  136.                 (setq first x))
  137.            (setq last x))) ) )
  138.  
  139. ;(defun set-exclusive-or (list1 list2 &rest rest &key test test-not key)
  140. ;  (declare (ignore test test-not key))
  141. ;  (append (apply #'set-difference list1 list2 rest)
  142. ;          (apply #'set-difference list2 list1 rest)))
  143. (defun set-exclusive-or (list1 list2 &rest rest &key test test-not key)
  144.   (declare (ignore test test-not key))
  145.   (nconc (apply #'set-difference list1 list2 rest)
  146.          (apply #'set-difference list2 list1 rest)))
  147.  
  148. ;(defun nset-exclusive-or (list1 list2 &rest rest &key test test-not key)
  149. ;  (declare (ignore test test-not key))
  150. ;  (nconc (apply #'set-difference list1 list2 rest)
  151. ;         (apply #'nset-difference list2 list1 rest)))
  152. (defun nset-exclusive-or (list1 list2 &rest rest &aux first last fint lint)
  153.   (do ((x list1 (cdr x)))
  154.       ((null x) (if lint (rplacd lint nil))
  155.                 (if last
  156.             (progn (rplacd last
  157.                    (apply #'nset-difference list2 fint rest))
  158.                (return first))
  159.             (return (apply #'nset-difference list2 fint rest))))
  160.     (or (consp x) (error "NSET-EXCLUSIVE-OR not passed a list"))
  161.     (if (apply #'member1 (car x) list2 rest)
  162.     (progn (if lint (rplacd lint x)
  163.                 (setq fint x))
  164.            (setq lint x))
  165.     (progn (if last (rplacd last x)
  166.                 (setq first x))
  167.            (setq last x))) ) )
  168.  
  169. (defun subsetp (list1 list2 &rest rest &key test test-not key)
  170.   (declare (ignore test test-not key))
  171.   (do ((l list1 (cdr l)))
  172.       ((null l) t)
  173.     (or (consp l) (error "SUBSETP not passed a list"))
  174.     (if (not (apply #'member1 (car l) list2 rest)) (return nil))))
  175.  
  176.  
  177.